package com.tl.commonlibrary.ui.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.tl.commonlibrary.R;


/**
 * 上传图片弹窗
 */

public class PhotoSelectPopupWindow extends PopupWindow implements View.OnClickListener {
    private Activity mContext;

    public PhotoSelectPopupWindow(Activity context) {
        this.mContext = context;
        init();
    }

    private void init() {
        setAnimationStyle(R.style.popup_show_Animation);
        // 设置弹出窗体的宽和高
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        //解决华为手机window被navigationBar挡住
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        View rootView = View.inflate(mContext, R.layout.layout_select_photo, null);
        rootView.findViewById(R.id.re_pai).setOnClickListener(this);
        rootView.findViewById(R.id.re_photo).setOnClickListener(this);
        rootView.findViewById(R.id.re_cancel).setOnClickListener(this);
        setContentView(rootView);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                lp.alpha = 1.0f;
                mContext.getWindow().setAttributes(lp);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.re_pai) {
            dismiss();
            if (onPhotoSelectListener != null) {
                onPhotoSelectListener.takePhoto();
            }
        } else if (id == R.id.re_photo) {
            dismiss();
            if (onPhotoSelectListener != null) {
                onPhotoSelectListener.selectPhoto();
            }
        } else if (id == R.id.re_cancel) {
            dismiss();
        }

    }

    public void setOnPhotoSelectListener(OnPhotoSelectListener onPhotoSelectListener) {
        this.onPhotoSelectListener = onPhotoSelectListener;
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mContext.getWindow().setAttributes(lp);
    }

    private OnPhotoSelectListener onPhotoSelectListener;

    public interface OnPhotoSelectListener {
        public void takePhoto();

        public void selectPhoto();
    }

}
